##########################################################################
#  Copyright (C) 2007 Robert Connolly <rob_connolly@mailshack.com>       #
#                                                                        #
#  This program is free software; you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation; either version 3 of the License, or     #
#  (at your option) any later version. See file COPYING for details.     #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program; if not, write to the                         #
#  Free Software Foundation, Inc.,                                       #
#  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
##########################################################################

import gtk
import cairo
import rsvg
from PIL import Image

from cairolets.core import Display
from cairolets.sensors.RhythmboxSensor import RhythmboxSensor

class Rhythmlet(Display):
	"""<name>Rhythmlet</name>
	   <description>A Display to interact with/control the Rhythmbox media player</description>
	   <author email='rob_connolly@mailshack.com'>Robert Connolly</author>
	   <website>http://cairolets.googlecode.com</website>"""
	
	def __init__(self, name):
		Display.__init__(self, name)
		
		self.sensor = RhythmboxSensor()
		self.sensor.connect('data-changed', self.sensor_update)
		
		self.svg = rsvg.Handle("/home/robert/Desktop/cairolets/displays/Rhythmlet/Background.svg")
		
		self.cover_img = gtk.Image()
		self.cover_img.set_alignment(0.5, 0.3)
		self.add(self.cover_img)
		self.cover_img.show()
		
		self.sensor_update(self.sensor, "all")
		
	def do_expose(self, ctx):
		self.resize(self.svg.get_property('width'), self.svg.get_property('height'))
		self.svg.render_cairo(ctx)
		
		ctx.set_source_rgba(0.0, 0.0, 0.0, 0.80)
		ctx.set_font_size(12)
		
		ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
		xbearing, ybearing, width, height, xadvance, yadvance = (ctx.text_extents(self.sensor.get_data('title')))
		x = (self.svg.get_property('width')/2) - (width/2) - 3
		ctx.move_to(x, 160)
		ctx.show_text(self.sensor.get_data('title'))
		
		ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL)
		xbearing, ybearing, width, height, xadvance, yadvance = (ctx.text_extents(self.sensor.get_data('artist')))
		x = (self.svg.get_property('width')/2) - (width/2) - 3
		ctx.move_to(x, 173)
		ctx.show_text(self.sensor.get_data('artist'))
		
		ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL)
		xbearing, ybearing, width, height, xadvance, yadvance = (ctx.text_extents(self.sensor.get_data('album')))
		x = (self.svg.get_property('width')/2) - (width/2) - 3
		ctx.move_to(x, 186)
		ctx.show_text(self.sensor.get_data('album'))
		
	def do_window_mask(self):
		self.svg.render_cairo(self.mask)
		
	def sensor_update(self, sensor, key):
		if key == "all":
			if self.sensor.get_data('cover'):
				buf = gtk.gdk.pixbuf_new_from_file_at_size(self.sensor.get_data('cover'), 128, 128)
				self.cover_img.set_from_pixbuf(buf)
		self.queue_draw()
		
		
